package com.huiminpay.merchant.controller;

import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.domain.PageVO;
import com.huiminpay.common.cache.util.QRCodeUtil;
import com.huiminpay.merchant.api.MerchantService;
import com.huiminpay.merchant.common.intercept.util.SecurityUtil;
import com.huiminpay.merchant.dto.MerchantDto;
import com.huiminpay.merchant.dto.StoreDTO;
import com.huiminpay.transaction.api.dto.QRCodeDto;
import com.huiminpay.transaction.api.service.TransactionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@Api(value = "商户平台‐门店管理", tags = "商户平台‐门店管理", description = "商户平台‐门店的增删改查")
@RestController
public class StoreController {
    @Reference
    private MerchantService merchantService;
    @Reference
    private TransactionService transactionService;
    @Value("${huiminpay.c2b.subject}")
    private String subject;
    @Value("${huiminpay.c2b.body}")
    private String body;
    @ApiOperation("分页条件查询商户下门店")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "int",
                    paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = true, dataType =
                    "int", paramType = "query")})
    @GetMapping("/my/stores/merchants/page")
    public PageVO<StoreDTO> queryStoreByPage(@RequestParam Integer pageNo,@RequestParam Integer pageSize){
        Long merchantId = SecurityUtil.getMerchantId();
        StoreDTO storeDTO = new StoreDTO();
        storeDTO.setMerchantId(merchantId);
        return  merchantService.queryStoreByPage(storeDTO,pageNo,pageSize);
    }

    @ApiOperation("生成商户应用门店二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "商户应用id", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "storeId", value = "商户门店id", required = true, dataType = "String", paramType = "path"),
    })
    @GetMapping(value = "/my/apps/{appId}/stores/{storeId}/app-store-qrcode")
    public String createCScanBStoreQRCode(@PathVariable String appId, @PathVariable Long storeId) throws BusinessException {
        Long merchantId = SecurityUtil.getMerchantId();
        MerchantDto merchantDto = merchantService.queryMerchantById(merchantId);
        QRCodeDto qrCodeDto=new QRCodeDto();
        qrCodeDto.setMerchantId(merchantId);
        String formatsubject = String.format(subject, merchantDto.getMerchantName());
        qrCodeDto.setSubject(formatsubject);
        String formatbody = String.format(body, merchantDto.getMerchantName());
        qrCodeDto.setBody(formatbody);
        qrCodeDto.setAppId(appId);
        qrCodeDto.setStoreId(storeId);
        String storeQRCodeUrl = transactionService.createStoreQRCode(qrCodeDto);
        log.info("[merchantId:{},appId:{},storeId:{}]createCScanBStoreQRCode is {}",merchantId,appId,storeId,storeQRCodeUrl);
        QRCodeUtil qrCodeUtil = new QRCodeUtil();
        String qrCode = null;
        try {
            qrCode = qrCodeUtil.createQRCode(storeQRCodeUrl, 200, 200);
            return qrCode;
        } catch (IOException e) {
            throw new BusinessException(CommonErrorCode.E_200007);
        }
    }
}
